unit Unit2; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, db,
  DBGrids, Menus, Unit1, Unit3, Unit4, LR_DBSet, LR_Class;

type

  { TForm2 }

  TForm2 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
    procedure MenuItem11Click(Sender: TObject);
    procedure ordenar_grid();
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
  private
    nombre : String;
    contacto : String;
  public
    { public declarations }
  end; 

var
  Form2: TForm2; 

implementation

{ TForm2 }

procedure TForm2.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select prv_cdg, prv_nombre, prv_contacto, prv_direccion, prv_email, '
      + 'prv_telefono from proveedor where prv_nombre ilike ''%' + nombre
      + '%'' and prv_contacto ilike ''%' + contacto + '%'' order by prv_nombre';

  SQLQuery1.Open;
  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Nombre del Proveedor';
  DBGrid1.Columns[2].Title.Caption := 'Contacto';
  DBGrid1.Columns[3].Title.Caption := 'Dirección';
  DBGrid1.Columns[4].Title.Caption := 'e-mail';
  DBGrid1.Columns[5].Title.Caption := 'Teléfono';

  DBGrid1.Columns[0].Width := 55;
  DBGrid1.Columns[1].Width := 150;
  DBGrid1.Columns[2].Width := 150;
  DBGrid1.Columns[3].Width := 220;
  DBGrid1.Columns[4].Width := 220;
  DBGrid1.Columns[5].Width := 145;
end;

procedure TForm2.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
  
  nombre := '%';
  contacto := '%';
  
  ordenar_grid;
end;

procedure TForm2.MenuItem11Click(Sender: TObject);
begin
  frReport1.LoadFromFile('listado_proveedores.lrf');
  frReport1.ShowReport;
end;

procedure TForm2.MenuItem10Click(Sender: TObject);
begin
  FormShow;
end;

procedure TForm2.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm2.DBGrid1CellClick(Column: TColumn);
var
  codigo : string;
  
begin
  codigo := SQLQuery1.FieldByName('prv_cdg').AsString;
  Form4.codigo := codigo;
  Form4.ShowModal;
  ordenar_grid();
end;

procedure TForm2.MenuItem2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm2.MenuItem4Click(Sender: TObject);
begin
  Form3.ShowModal;
  FormShow;
end;

procedure TForm2.MenuItem5Click(Sender: TObject);
var
  proveedor : String;
  QueryString : String;
begin
  proveedor := InputBox('Baja de Proveedor', 'Ingrese el código de proveedor a eliminar:', '');
  
  if proveedor <> '' then
  begin
    QueryString := ' delete from proveedor where prv_cdg = ' + proveedor;
    
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
    
    ShowMessage('BAJA EXITOSA');

    FormShow;
  end;
end;

procedure TForm2.MenuItem6Click(Sender: TObject);
begin
  ShowMessage('Click en el proveedor a modificar...');
end;

procedure TForm2.MenuItem8Click(Sender: TObject);
begin
  nombre := InputBox('Filtro de Proveedores por Nombre', 'Ingrese parte del nombre del proveedor:', '');
  
  if nombre <> '' then
    ordenar_grid;
end;

procedure TForm2.MenuItem9Click(Sender: TObject);
begin
  contacto := InputBox('Filtro de Proveedores por Contacto', 'Ingrese parte del contacto del proveedor:', '');

  if contacto <> '' then
    ordenar_grid;
end;

initialization
  {$I unit2.lrs}

end.

